#include "gamecontrol.h"

GameControl* GameControl :: instance = nullptr;
GameControl::GameControl(QWidget* parent) : QObject(parent)
{
    //添加平台
    //顶层
    addPlatform(40,10,1250,40);
    //第一层
    addPlatform(280,150,150,30);
    addPlatform(380,180,100,130);
    addPlatform(480,210,70,100);
    addPlatform(550,210,370,40);
    addPlatform(920,180,110,70);
    addPlatform(1030,210,250,40);
    //第二层
    addPlatform(40,250,141,100);
    addPlatform(210,380,440,40);
    addPlatform(650,330,300,90);
    addPlatform(980,380,190,70);
    //第三层
    addPlatform(170,520,480,40);
    addPlatform(650,520,70,60);
    addPlatform(720,550,371,40);
    addPlatform(1100,550,140,70);
    addPlatform(1230,550,71,110);
    //第四层
    addPlatform(40,690,540,40);
    addPlatform(580,690,60,100);
    addPlatform(640,750,151,40);
    addPlatform(790,790,101,30);
    addPlatform(990,750,171,40);
    //四层半
    addPlatform(40,820,400,30);
    //第五层
    addPlatform(1200,860,141,90);
    //小平台
    addPlatform(50,615,100,25);
}
void GameControl::GameInit()
{
    //设置视图的父亲为窗口
    mGameView.setParent(Widget::widget);

    LoadStartScene();//开始场景初始化

    //定时器初始化
    mPlayerMoveTimer = new QTimer(this);
    mJumpTimer = new QTimer(this);
    mFormTimer = new QTimer(this);
    mGameTimer = new QTimer(this);

    lcdnumber = new QLCDNumber;

    connect(mPlayerMoveTimer,&QTimer::timeout,this,[this]()
    {
        mManBlue.Move();
        mManBlue.changePixmap();
        mManBlue.diamondCollisions();

        mManRed.Move();
        mManRed.changePixmap();
        mManRed.diamondCollisions();
    });

    connect(mJumpTimer,&QTimer::timeout,this,[this]()
            {
        mManBlue.updatePosition();
        mManBlue.downCollisions();

        mManRed.updatePosition();
        mManRed.downCollisions();
    });

    connect(mFormTimer,&QTimer::timeout,this,[this]()
            {
        mManBlue.detectCollisions();
        mManRed.detectCollisions();
    });

    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::GameOver);

    connect(mGameTimer,&QTimer::timeout,this,[this]()
            {
        static int num =180;

        if(num > 0)
            {
            num--;
            lcdnumber->display(num);
        }
        else
            {
            QMessageBox::critical(nullptr,"时间结束","请重新开始");
            mManBlue.setPos(20,879);
            mManRed.setPos(70,879);
            num = 180;
            lcdnumber->display(num);
        }
    });
}
void GameControl::LoadStartScene()
{
    mStartScene.setSceneRect(QRect(0,0,1347,1009));
    mStartScene.addPixmap(QPixmap(":/1/Image/BackgroundStart.png"));

    auto startbtn = new QToolButton;
    startbtn->setAutoRaise(true);
    startbtn->setIcon(QIcon(":/1/Image/ButtonStart.png"));
    startbtn->setIconSize(QSize(353,46));
    startbtn->move(470,500);

    auto startlabel = new QLabel;
    startlabel->setText("使用WASD移动冰人，使用5123移动火人");
    startlabel->setFont(QFont("",20,1));
    startlabel->move(330,750);

    //开始游戏点击
    connect(startbtn,&QToolButton::clicked,this,[this](){

        //加载游戏场景
        this->LoadGameScene();
        //开始游戏
        this->GameStart();
    });

    mStartScene.addWidget(startbtn);
    mStartScene.addWidget(startlabel);

    //设置当前场景为开始场景
    mGameView.setScene(&mStartScene);
    mGameView.show();
}
void GameControl::LoadGameScene()
{
    mGameScene.setSceneRect(QRect(0,0,1347,1009));

    //设置坐标
    mGameScene.setSceneRect(QRect(0,0,1347,1009));
    mWaterRed.setPos(620,937);
    mWaterBlue.setPos(890,907);
    mWaterGreen.setPos(840,715);
    mStrategyUpdown.setPos(30,590);
    mDoorBlue.setPos(1220,140);
    mDoorRed .setPos(1140,140);

    lcdnumber->setGeometry(590,5,160,40);
    lcdnumber->display(180);

    //添加图片
    mBackground.setPixmap(QPixmap(":/1/Image/Background.png"));
    mWaterRed.setPixmap(QPixmap(":/1/Image/WaterRed.png"));
    mWaterBlue.setPixmap(QPixmap(":/1/Image/WaterBlue.png"));
    mWaterGreen.setPixmap(QPixmap(":/1/Image/WaterGreen.png"));
    mStrategyUpdown.setPixmap(QPixmap(":/1/Image/StrategyUpdown.png"));
    mDoorBlue.setPixmap(QPixmap(":/1/Image/DoorBlue4.png"));
    mDoorRed.setPixmap(QPixmap(":/1/Image/DoorRed4.png"));

    //图片元素添加到场景
    mGameScene.addItem(&mBackground);
    mGameScene.addItem(&mWaterRed);
    mGameScene.addItem(&mWaterBlue);
    mGameScene.addItem(&mWaterGreen);
    mGameScene.addItem(&mStrategyUpdown);
    mGameScene.addItem(&mDoorBlue);
    mGameScene.addItem(&mDoorRed);

    mGameScene.addWidget(lcdnumber);

    //钻石创建
    QPixmap DiamondImageBlue(":/1/Image/DiamondBlue.png");
    QPixmap DiamondImageRed(":/1/Image/DiamondRed.png");

    QPoint pos1(820,150);
    QPoint pos2(90,180);
    QPoint pos3(830,480);
    QPoint pos4(950,880);

    QPoint pos5(230,460);
    QPoint pos6(310,90);
    QPoint pos7(570,150);
    QPoint pos8(670,895);

    Diamond* diamond1 = new Diamond(pos1,DiamondImageBlue,Diamond::DT_Blue);
    Diamond* diamond2 = new Diamond(pos2,DiamondImageBlue,Diamond::DT_Blue);
    Diamond* diamond3 = new Diamond(pos3,DiamondImageBlue,Diamond::DT_Blue);
    Diamond* diamond4 = new Diamond(pos4,DiamondImageBlue,Diamond::DT_Blue);

    Diamond* diamond5 = new Diamond(pos5,DiamondImageRed,Diamond::Dt_Red);
    Diamond* diamond6 = new Diamond(pos6,DiamondImageRed,Diamond::Dt_Red);
    Diamond* diamond7 = new Diamond(pos7,DiamondImageRed,Diamond::Dt_Red);
    Diamond* diamond8 = new Diamond(pos8,DiamondImageRed,Diamond::Dt_Red);

    mGameScene.addItem(diamond1);
    mGameScene.addItem(diamond2);
    mGameScene.addItem(diamond3);
    mGameScene.addItem(diamond4);
    mGameScene.addItem(diamond5);
    mGameScene.addItem(diamond6);
    mGameScene.addItem(diamond7);
    mGameScene.addItem(diamond8);

    mDiamondsBlue.append(diamond1);
    mDiamondsBlue.append(diamond2);
    mDiamondsBlue.append(diamond3);
    mDiamondsBlue.append(diamond4);

    mDiamondsRed.append(diamond5);
    mDiamondsRed.append(diamond6);
    mDiamondsRed.append(diamond7);
    mDiamondsRed.append(diamond8);

    p1 = &mManBlue;
    p1 = &mManRed;

    mGameScene.addItem(&mManRed);
    mGameScene.addItem(&mManBlue);

    //设置当前场景为游戏场景
    mGameView.setScene(&mGameScene);
    mGameView.show();
}
//游戏开始
void GameControl::GameStart()
{
    //开始定时器
    mPlayerMoveTimer->start(GameDefine::playerMoveTimer);
    mJumpTimer->start(GameDefine::playerMoveTimer);
    mFormTimer->start(GameDefine::platformTimer);
    mGameTimer->start(GameDefine::gameTimer);
}
//游戏结束
void GameControl::GameOver()
{
    this->setParent(Widget::widget);
    QRectF mManBlueRect = mManBlue.boundingRect().translated(mManBlue.pos());
    mManBlueRect = QRectF(mManBlue.x(),mManBlue.y(),25,25);

    QRectF mManRedRect = mManRed.boundingRect().translated(mManRed.pos());
    mManRedRect = QRectF(mManRed.x(),mManRed.y(),25,25);

    QRectF mDoorBlueRect = mDoorBlue.boundingRect().translated(mDoorBlue.pos());
    QRectF mDoorRedRect = mDoorRed.boundingRect().translated(mDoorRed.pos());
    if( mManBlueRect.intersects(mDoorBlueRect) &&mManRedRect.intersects(mDoorRedRect))
    {
        mSuccessScene.addPixmap(QPixmap(":/1/Image/Success.png"));
        mGameView.setScene(&mSuccessScene);
        mPlayerMoveTimer->stop();
        mFormTimer->stop();
        mGameView.show();
    }
}
//添加平台到场景
void GameControl::addPlatform(double x,double y,int width,int height)
{
    mGameScene.addRect(x,y,width,height);
    platforms.append(QRectF(x,y,width,height));
}
QGraphicsScene& GameControl::getGameScene()
{
    return mGameScene;
}

